import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { WarpperService } from '../../shared/service/Warpper.service';
import { FunctionService } from '../../shared/service/function.service';
import { LoginService } from '../../shared/service/login.service';


@Component({
  selector: 'app-warpper',
  templateUrl: './warpper.component.html',
  styleUrls: ['./warpper.component.scss']
})
export class WarpperComponent implements OnInit {

  yearsList = [];

  zero: number;

  // 当前登陆用户
  user: any;

  constructor(
    private router: Router,
    private warpperService: WarpperService,
    private functionService: FunctionService,
    private loginService: LoginService
  ) {
    this.zero = 0;
  }

  ngOnInit() {

    // 检查是否有用户登录
    this.ifLogin();

    // 设置页面默认数据
    // 存在用户才开始默认请求
    if (this.user) {
      this.defaultData();
    }

  }

  /*****
  *  检查是否有用户登录
  *  @param  :  (参数)
  *  @return :  (返回值说明)
  *****/
  ifLogin() {
    this.user = this.loginService.user;
    if (!this.user) {
      this.router.navigateByUrl('/login');
    }
  }

  defaultData() {
    const that = this;
    this.warpperService.getYearsData()
      .then((data) => {
        this.yearsList = this.functionService.clearObjToArray(data);
      });
  }

  getYears(years) {

    event.stopPropagation();

    this.warpperService.getMonthData(years.id)
        .then((data)=>{
          years.children = this.functionService.clearObjToArray(data);
        })
  }

  getMonth(month) {

    event.stopPropagation();

    this.warpperService.getDayData(month.id)
        .then((data)=>{
          month.children = this.functionService.clearObjToArray(data);
        })
  }

  getEvent(dEvt) {

    event.stopPropagation();

    this.warpperService.getEventData(dEvt.id)
        .then((data)=>{
          dEvt.children = this.functionService.clearObjToArray(data);
        })
  }

}
